﻿/**
 * Copyright © 2010-2012 by Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SimpleProblem.as
 */
package org.robcash.apps.flashcards.vo
{
    public class SimpleProblem implements IMathProblem
    {
        private var _correctAnswer:Number = 0;
        private var _operators:Array;
        private var _firstNumber:int = 0;
        private var _operation:IMathOperation;
        private var _operands:Array;
        private var settingNumber:Boolean = false;
        private var settingOperands:Boolean = false;
        private var _secondNumber:int = 0;

        public function SimpleProblem(firstNumber:int, secondNumber:int, operation:IMathOperation)
        {
            _firstNumber = firstNumber;
            _secondNumber = secondNumber;
            _operation = operation;
            operands = [firstNumber, secondNumber];
            operators = [operation];
        }

		[Bindable]
        public function get firstNumber() : int
        {
            return _firstNumber;
        }

        public function set firstNumber(param1:int) : void
        {
            _firstNumber = param1;
            if (!settingOperands)
            {
                settingNumber = true;
                operands = [firstNumber, secondNumber];
                settingNumber = false;
            }
        }

		[Bindable]
        public function get secondNumber() : int
        {
            return _secondNumber;
        }

        public function set secondNumber(param1:int) : void
        {
            _secondNumber = param1;
            if (!settingOperands)
            {
                settingNumber = true;
                operands = [firstNumber, secondNumber];
                settingNumber = false;
            }
        }

		[Bindable]
        public function get operation() : IMathOperation
        {
            return _operation;
        }

        public function set operation(operation:IMathOperation) : void
        {
            _operation = operation;
            return;
        }

		[Bindable]
        public function get correctAnswer() : Number
        {
            return _correctAnswer;
        }

        public function set correctAnswer(answer:Number) : void
        {
            _correctAnswer = answer;
        }

		[Bindable]
        public function get operands() : Array
        {
            return _operands;
        }

        public function set operands(operands:Array) : void
        {
            if (operands.length != 2)
            {
                throw new Error("SimpleProblem requires exactly 2 operands");
            }
            _operands = operands;
            if (!settingNumber)
            {
                settingOperands = true;
                firstNumber = operands[0];
                secondNumber = operands[1];
                settingOperands = false;
            }
            correctAnswer = operation.evaluate(firstNumber, secondNumber);
        }

		[Bindable]
        public function get operators() : Array
        {
            return _operators;
        }

        public function set operators(operators:Array) : void
        {
            if (operators.length != 1)
            {
                throw new Error("SimpleProblem requires exactly 1 operator");
            }
            _operators = operators;
        }

    }
}
